﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Com.CMS263.Util;
using Com.CMS263.PO;

namespace Com.CMS263.BLL
{
    public class ArtPageTransformbyid:ITag
    {
        /// <summary>
        /// 要解析的含有标签的html代码
        /// </summary>
        private string ctx = null;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="ctx">页面内容</param>
        /// 
        List<string> tags = new List<string>();
        List<string> tags2 = new List<string>();
        TagUtil tu = new TagUtil();
        ArtBase ab = new ArtBase();
        ArtBaseBLL bll = new ArtBaseBLL();
        ExtValueBLL evbll = new ExtValueBLL();
        //public ArtPageTransformbyid(string ctx)
        //{
        //    this.ctx = ctx;
        //}
        /// <summary>
        /// 得到替换标签后的内容
        /// </summary>
        /// <returns>返回替换后的结果</returns>
        /// 
        int ta = 0;
        public string Replace(string ctx)
        {
            try
            {
                ta = tu.GetTag(ctx, "cms:onepagebyid", tags).Count;
            }
            catch { }
            if (ta > 0)
            {
                tags = tu.GetTag(ctx, "cms:onepagebyid", tags);
                foreach (string tag in tags)
                {
                    string innnerListHTML = tu.GetInnerHTML("cms:onepagebyid", tag);
                    /// <summary>
                    /// 文章分类编号
                    /// </summary>
                    int attrid = 0;
                    //得到包含属性的标签字符串
                    int end = tag.IndexOf(">");
                    string arrtStr = tag.Substring(1, end - 1);
                    //标签呗替换后生成的html
                    string tagInnerHTML = "";
                    //解析标签得到所有的属性
                    //获取的id
                    int id = 0;
                    Dictionary<string, string> attrDic = tu.Getattribute(arrtStr);
                    //如果是通过传递过来的参数取分类的那么就用参数得到编号
                    bool f=true;
                    try
                    {
                        f = string.IsNullOrEmpty(attrDic["pageid"]);
                    }
                    catch { }
                    if (f == false)
                    {
                        string str = attrDic["pageid"].Replace("\"", "");
                            id = int.Parse(str);
                    }
                    else
                    {
                        string para = System.Web.HttpContext.Current.Request[attrDic["paraname"].Replace("\"", "")];
                        if (!string.IsNullOrEmpty(para))
                        {
                            id = int.Parse(para);
                        }
                        else
                        {
                            throw new Exception("分类参数名称错误，请使用正确的参数名称传递参数");
                        }
                    }
                   

                    //根据获取id编号取得所要的数据
                    ab = bll.GetArtBaseById(id);
                    string alllist = "";
                    //解析里面的<cms:attribute  field=""  length=""/>标签并用一条数据中的各个属性替换标签
                    List<string> attrs = new List<string>();
                    attrs = tu.GetTag(tag, "cms:attribute", attrs);

                    //循环累加的连接字符串
                    string innerHTML = tu.GetInnerHTML("cms:onepagebyid", tag);
                    foreach (string attr in attrs)
                    {

                        Dictionary<string, string> attrlist = tu.Getattribute(attr);
                        string attrvalue = "";
                        int filedID = 0;
                        string field = attrlist["field"].Replace("\"", "").ToLower();
                        int length = 0;


                        if (field.ToLower() == "title")
                        {
                            attrvalue = ab.title.ToString();
                        }
                        else if (field.ToLower() == "adddate")
                        {
                            string v = "";
                            attrvalue = ab.addDate.ToString();

                            if (attrlist.TryGetValue("formate", out v))
                            {
                                v = attrlist["formate"].Replace("\"", "");
                                attrvalue = ab.addDate.ToString(v);
                            }
                        }
                        else if (field.ToLower() == "menuid")
                        {
                            attrvalue = ab.menuId.ToString();
                        }
                        else if (field.ToLower() == "id")
                        {
                            attrvalue = ab.id.ToString();
                        }

                      
                        else
                        {
                            //当是属性编号时，将属性编号变成整形，否则报错
                            try
                            {
                                filedID = int.Parse(field);
                                attrvalue = evbll.GetValueById(ab.id, filedID);
                            }
                            catch
                            {
                                throw new Exception("属性值错误，请检查属性值：" + field);
                            }
                        }

                        //如果有长度要求，那么进行截取
                        if (!string.IsNullOrEmpty(attrlist["length"]))
                        {
                            string l = attrlist["length"].Replace("\"", "");
                            if (!string.IsNullOrEmpty(l))
                            {
                                length = Convert.ToInt32(l);
                                attrvalue = StringUtil.GetText(attrvalue, length);
                            }
                        }
                        innerHTML = innerHTML.Replace(attr, attrvalue);
                    }
                    alllist += innerHTML;
                    ctx = ctx.Replace(tag, alllist);

                }
            }

            return ctx;
        }

    }
}
